package com.jkc.xh.fragments;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jkc.xh.R;
import com.jkc.xh.adapters.TodayWeituoAdapter;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.AssetInfoResponse;
import com.jkc.xh.network.models.EntrustListResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.headview.HeadMyMoney;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 当日委托
 */
@EFragment(R.layout.fragment_today_weituo)
public class TodayWeituoFragment extends BasePullRecyclerFragment {

    @ViewById(R.id.nav_bar)
    NavBar activityTitleName;
    @ViewById(R.id.head_money)
    HeadMyMoney headMoney;

    private TodayWeituoAdapter mAdapter;
    private int pageIndex = 0;
    private int pageTotal = 0;


    @Override
    protected void initView(PullRecyclerView recyclerView) {
        mAdapter = new TodayWeituoAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);

        activityTitleName.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                requestData(true);
            }
        });
    }


    @AfterViews
    public void loadData() {
        requestData(true);
    }

    private void requestData(final boolean isRefresh) {
        addJob(TraderManager.queryAssetInfo(new RestCallBack<AssetInfoResponse>() {


            @Override
            public void success(AssetInfoResponse assetInfoResponse) {
                headMoney.loadInfo(assetInfoResponse);
            }

            @Override
            public void failure(RestError error) {

            }
        }));
        if (isRefresh) {
            pageIndex = 0;
            getPullRecyclerView().autoRefresh();
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        addJob(TraderManager.entrustList(pageIndex, new RestCallBack<EntrustListResponse>() {
            @Override
            public void success(EntrustListResponse response) {
                pageTotal = response.pages;
                if (response.list == null || response.list.size() == 0) {
//                    ToastUtil.showToastShort("数据加载完毕");
                    finishLoad(false);
                    return;
                }
                if (isRefresh) {
                    mAdapter.replaceWith(response.list);
                } else {
                    mAdapter.addAll(response.list);
                }
                if (response.list.size() < Constant.PAGE_SIZE) {
                    finishLoad(false);
                } else {
                    finishLoad(true);
                }
            }

            @Override
            public void failure(RestError error) {

            }
        }));


    }

    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
    }
}
